/*
 * 格式化时间 xxxx-xx-xx
 * @params {string/number}  timestamp   时间戳
 * @params {string}  sep    分隔符，默认'-'
 */
let formatTime1 = function(timestamp, sep='-') {
	if(!timestamp) {
		return '';
	}
	let time = parseInt(timestamp);
	if(typeof time === 'number') {
		time = new Date(time);
	    let year = time.getFullYear(),
	        mon = time.getMonth() + 1,
	        date = time.getDate();

	     mon = mon > 9 ? mon : '0' + mon;
	     date = date > 9 ? date: '0' + date;

	    return `${year}${sep}${mon}${sep}${date}`;
	}else {
	    return timestamp;
	}
} 
/*
 * 格式化时间 xxxx-xx-xx hh:mm
 * @params {string/number}  timestamp   时间戳
 */
 //转换时间戳
let formatTime2 = function(timestamp, sep=['-', ' ', ':']) {
	if(!timestamp) {
		return '';
	}
	let time = parseInt(timestamp);
	if(typeof time === 'number') {
		time = new Date(time);
	    let year = time.getFullYear(),
	        mon = time.getMonth() + 1,
	        date = time.getDate(),
	        hour = time.getHours(),
	        min = time.getMinutes();

	     mon = mon > 9 ? mon : '0' + mon;
	     date = date > 9 ? date: '0' + date;
	     hour = hour > 9 ? hour: '0' + hour;
	     min = min > 9 ? min: '0' + min;

	    return `${year}${sep[0]}${mon}${sep[0]}${date}${sep[1]}${hour}${sep[2]}${min}`;
	}else {
	    return timestamp;
	}
} 
/*
 * 转换为formdata数据格式
 */
let formData = function(obj) {
	let data = new FormData();
    for(let k in obj) {
        data.append(k, obj[k])
    }

    return data;
}

module.exports = {
	formatTime1: formatTime1,
	formData: formData,
	formatTime2: formatTime2,
}